﻿@model CTM.Web.Models.View.Comment.CommentViewModel
@using CTM.Web.Utils

@{
    var formId = string.Format("comment-form-of-{0}", Model.PostId);
    var listId = string.Format("comments-of-{0}", Model.PostId);
    //var txtId = string.Format("txtComment-of-{0}", Model.PostId);
    var funcName = string.Format("OnCommentSuccess-of-{0}", Model.PostId);
    var linkId = "no-comments-of-" + Model.PostId;
}

@using (Ajax.BeginForm("Create", "Comment",
                    null,
                    new AjaxOptions { HttpMethod = "POST", OnSuccess = funcName },
                    new { @class = "form-horizontal", id = formId }))
{
    @Html.AntiForgeryToken()
    @*@Html.ValidationSummary(true, "", new { @class = "text-danger" })*@
    @Html.HiddenFor(model => model.PostId)

    <div class="media" style="padding: 0 65px; margin-bottom: 10px;">
        <div class="media-left">
            <a href="@Url.Action("Index", "Home", new { id = GlobalVariables.CurrentUser.Id })">
                <img src="@GlobalVariables.CurrentUser.Avatar" width="48" height="48" alt="" class="user-avatar">
            </a>
        </div>
        <div class="media-body">
            @Html.EditorFor(model => model.Content, new { htmlAttributes = new { @class = "form-control input-for-comment", @placeholder = "Write a comment...", data_form_id = formId } })
            @Html.ValidationMessageFor(model => model.Content, "", new { @class = "text-danger" })
        </div>
    </div>
}

<script>
    window['@funcName'] = function (data) {
        $('#@formId')[0].reset();  // reset form

        $('#@linkId').text(data.count);

        // update comment list
        $('#@listId').load('@Url.Action("Index", "Comment", new { id = Model.PostId })', function () {

            // open 'Delete Comment' popup
            $('.rbvh-btn-remove-comment').on('click', function (evt) {

                var $divBody = $('#DeleteCommentModalBody'),
                    $divForm = $('#delete-comment-form'),
                    url = $(this).data('url');

                $.get(url, function (data) {
                    $divBody.html(data);
                    $.validator.unobtrusive.parse($divForm);
                });
            });

            $('#DeleteCommentModal').on('hidden.bs.modal', function () {
                $('#DeleteCommentModalBody').html('');
            });

        });
    }
</script>